
/*
 * @Author: Salaing
 * @Date: 2024-06-30 21:36:50
 * @LastEditors: Salaing
 */
import $http from 'api';
import { message } from 'antd'

export default {
    namespace: 'department',
    state: {
        departmentList: [],
        departmentDetail: null,
        showModalDialog: false,
    },
    reducers: {
        // 保存部门列表
        saveDepartmentList: (state, { payload }) => ({ ...state, ...payload }),
        // 保存部门详情
        saveDepartmentDetail: (state, { payload }) => ({ ...state, ...payload }),
        // 
        saveShowModalDialog: (state, { payload }) => ({ ...state, ...payload }),
    },
    effects: {
        //- 获取部门列表
        *_initDepartmentList({ payload }, { put, call }) {
            const {
                data
            } = yield call($http.getDepartmentList, payload);


            // 去除 parentsLists 为 null 的数据
            let list = data.list.map((item) => {
                const newParentLists = item.parentLists.filter(item => item !== null)
                return {
                    ...item,
                    parentLists: newParentLists
                }
            })
            // 递归 去除children为null的数据 
            function removeNullsFromChildren(items) {
                return items.map((item) => {
                    // 如果当前项有children数组
                    if (Array.isArray(item.children)) {
                        // 过滤掉children中的null值，并递归处理剩余的children
                        item.children = removeNullsFromChildren(
                            item.children.filter((child) => child !== null),
                        );
                    }
                    // 返回修改后的项
                    return item;
                })
            }
            removeNullsFromChildren(list)


            yield put({ type: 'saveDepartmentList', payload: { departmentList: list } });

        },
        //- 获取部门详情
        *_getDepartmentDetail({ payload }, { put, call }) {
            const { data, } = yield call($http.getDepartmentDetail, payload);

            let children = data.children.filter(item => item !== null)
            let staffList = data.staffList.filter(item => item !== null)
            data.children = children
            data.staffList = staffList


            yield put({
                type: 'saveDepartmentDetail',
                payload: { departmentDetail: data },
            });
            yield put({
                type: 'saveShowModalDialog',
                payload: { showModalDialog: true },
            });
        },
        //- 新增部门
        * _addDepartment({ payload }, { put, call }) {
            const { code, msg } = yield call($http.addDepartment, payload);
            if (code) return message.success(msg)
            yield put({ type: '_initDepartmentList', payload: {} });
        },
        //- 更新部门
        //- 修改部门信息
        *updateDepartmentDetail({ payload }, { put, call }) {
            const { code, msg } = yield call($http.updateDepartment, payload);
            if (code) return;
            message.success(msg);
            yield put({
                type: '_initDepartmentList',
                payload: {},
            });
            yield put({
                type: '_getDepartmentDetail',
                payload: { _id: payload._id },
            });
        },
    },

}
